﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using Model;

namespace DAL
{
    public class classifyDal
    {
        /// <summary>
        /// 通过商户ID查分类
        /// </summary>
        /// <param name="merid"></param>
        /// <returns></returns>
        public static List<ms_classify> Querybymerid(string merid)
        {
            string sql = "select * from ms_classify where merchant_id=@merchant_id and pid=0 and is_del=0 order by sort desc";
            MySqlParameter[] values = new MySqlParameter[1];
            values[0] = new MySqlParameter("merchant_id", merid);
            List<ms_classify> clist = new List<ms_classify>();
            MySqlDataReader rd = DBHelper.ExcutQuery(sql, values);
            while (rd.Read())
            {
                ms_classify classify = new ms_classify();
                classify.id = rd["id"].ToString();
                classify.merchant_id = rd["merchant_id"].ToString();
                classify.title = rd["title"].ToString();
                classify.sort = rd["sort"].ToString();
                clist.Add(classify);
            }
            rd.Close();
            return clist;
        }
        /// <summary>
        /// 新增分类名称
        /// </summary>
        /// <param name="merchantid"></param>
        /// <param name="title"></param>
        /// <param name="sort"></param>
        /// <returns></returns>
        public static int Addclassify(string merchantid,string title, int sort)
        {
            string sql = "insert into ms_classify(merchant_id,title,sort) values(@merchant_id,@title,@sort)";
            MySqlParameter[] values = new MySqlParameter[3];
            values[0] = new MySqlParameter("merchant_id", merchantid);
            values[1] = new MySqlParameter("title", title);
            values[2] = new MySqlParameter("sort", sort);
            return DBHelper.ExcutUpdate(sql, values);
        }
        /// <summary>
        /// 查看该分类是否存在
        /// </summary>
        /// <param name="title"></param>
        /// <param name="merchantid"></param>
        /// <returns></returns>
        public static int Querybyname(string title, string merchantid)
        {
            string sql = "select * from ms_classify where merchant_id=@merchant_id and title=@title and is_del=0";
            MySqlParameter[] values = new MySqlParameter[2];
            values[0] = new MySqlParameter("merchant_id", merchantid);
            values[1] = new MySqlParameter("title", title);
            MySqlDataReader rd = DBHelper.ExcutQuery(sql, values);
            int count = 0;
            if (rd.Read())
            {
                if (!string.IsNullOrEmpty(rd["id"].ToString()))
                    count = 1;
            }
            rd.Close();
            return count;
        }
        /// <summary>
        /// 更新排序
        /// </summary>
        /// <param name="sort"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public static int Updatesort(string sort, string id)
        {
            string sql = "update ms_classify set sort=@sort,update_time=@update_time where id=@id";
            MySqlParameter[] values = new MySqlParameter[3];
            values[0] = new MySqlParameter("id", id);
            values[1] = new MySqlParameter("sort", sort);
            values[2] = new MySqlParameter("update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            return DBHelper.ExcutUpdate(sql, values);
        }
        /// <summary>
        /// 更新名称
        /// </summary>
        /// <param name="title"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public static int Updatetitle(string title, string id)
        {
            string sql = "update ms_classify set title=@title,update_time=@update_time where id=@id";
            MySqlParameter[] values = new MySqlParameter[3];
            values[0] = new MySqlParameter("id", id);
            values[1] = new MySqlParameter("title", title);
            values[2] = new MySqlParameter("update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            return DBHelper.ExcutUpdate(sql, values);
        }
        /// <summary>
        /// 删除分类
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static int Delname( string id)
        {
            string sql = "update ms_classify set is_del=1,update_time=@update_time where id=@id";
            MySqlParameter[] values = new MySqlParameter[2];
            values[0] = new MySqlParameter("id", id);
            values[1] = new MySqlParameter("update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            return DBHelper.ExcutUpdate(sql, values);
        }
        /// <summary>
        /// 通过ID查分类名称
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static string Querybyid(string id)
        {
            string sql = "select * from ms_classify where id=@id";
            string classname = "";
            MySqlParameter[] values = new MySqlParameter[1];
            values[0] = new MySqlParameter("id", id);
            MySqlDataReader rd = DBHelper.ExcutQuery(sql, values);
            if (rd.Read())
            {
                classname = rd["title"].ToString();
            }
            rd.Close();
            return classname;
        }
    }
}
